﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using _6;
using static _6.Order_Apply;

namespace _6
{
    public partial class Form2 : Form
    {
        private Order_Apply.Order order1;
        private string custumor_name = "";
        private int order_id = 0;
        private int custumor_phonenumber = 0;
        private string custumor_home = "";
        private string product_name = "";
        private int product_amount = 0;
        private decimal product_unitprice = 0;
        private bool corder = false;

        public Form2()
        {
            InitializeComponent();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowidx = (e.RowIndex + 1).ToString();
            var centerFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            var headerBuunds = new System.Drawing.Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowidx, new Font("宋体", 11), SystemBrushes.ControlText, headerBuunds, centerFormat);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            @static.sorder = null;
            this.Close();
        }



        private void Button1_Click(object sender, EventArgs e)
        {
            if (custumor_name != null && custumor_home != null && custumor_phonenumber <= 9999999 && custumor_phonenumber >= 1111111 && order_id > 0)
            {
                order1 = new Order(new Custumor(custumor_name, custumor_phonenumber, custumor_home), order_id);
                corder = true;
            }
            else if (custumor_name == "")
            {
                Form3 f3 = new Form3("Please enter the custumort's name.");
                f3.ShowDialog();
            }
            else if (custumor_home == "")
            {
                Form3 f3 = new Form3("Please enter the custumort's home.");
                f3.ShowDialog();

            }
            else if (custumor_phonenumber < 1111111 || custumor_phonenumber > 9999999)
            {
                Form3 f3 = new Form3("Please enter the custumort's phone number between 1111111 and 9999999.");
                f3.ShowDialog();
            }
            else if (order_id <= 0)
            {
                Form3 f3 = new Form3("Please enter the order's ID (>0).");
                f3.ShowDialog();
            }

            else
            {
                Form3 f3 = new Form3("Please enter the whole information.");
                f3.ShowDialog();
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            if (textBox7.Text != "")
            {
                product_unitprice = decimal.Parse(textBox7.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (product_name != "" && product_unitprice > 0 && product_amount > 0 && corder)
            {
                order1.OrderDetails.Add(new OrderDetail { product = new Product(product_name, product_unitprice, product_amount) });
            }
            else if (!corder)
            {
                Form3 f3 = new Form3("Please enter the order's information first.");
                f3.ShowDialog();
            }
            else if (product_name == "")
            {
                Form3 f3 = new Form3("Please enter the product's name.");
                f3.ShowDialog();

            }
            else if (product_unitprice <= 0)
            {
                Form3 f3 = new Form3("Please enter the product's unitprice..");
                f3.ShowDialog();

            }
            else if (product_amount <= 0)
            {
                Form3 f3 = new Form3("Please enter the product's amount.");
                f3.ShowDialog();

            }
            else
            {
                Form3 f3 = new Form3("Please enter the product's information.");
                f3.ShowDialog();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if(textBox2.Text !="")
            {int i = int.Parse(textBox2.Text);
                order_id = i;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            custumor_name = textBox1.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if(textBox3.Text!="")
            { custumor_phonenumber = int.Parse(textBox3.Text); }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            custumor_home = textBox5.Text;
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            product_name = textBox6.Text;
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if(textBox8.Text!="")
            { product_amount = int.Parse(textBox8.Text); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (order1 != null && order1.OrderDetails.Count != 0)
            {
                @static.sorder = order1;
                this.Close();
            }
            else if (order1 != null && order1.OrderDetails.Count == 0)
            {
                Form3 f3 = new Form3("Please enter a product.");
                f3.ShowDialog();
            }
            else if (order1 == null)
            {
                Form3 f3 = new Form3("Please enter an order.");
                f3.ShowDialog();
            }
        }
    }
}
